﻿Imports Llama
Imports Plaice
Imports Telerik.WinControls
Imports Telerik.WinControls.UI
Imports System.Threading

Public Class FrmDelRevCloud
    ''' <summary>
    ''' 
    ''' </summary>
    ''' <returns></returns>
    Public Property sFilePath As String

    ''' <summary>
    ''' 指定要删除的云线，对应的symbol文件
    ''' </summary>
    ''' <returns></returns>
    Private Property sSymbolName As String
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        Dim iTotal As Integer = DrawingCol.Count
        Dim dr As DialogResult = RadMessageBox.Show("Are you sure to delete selected Revision Cloud?", "Delete Revision Cloud", MessageBoxButtons.YesNo, RadMessageIcon.Question)
        If dr = DialogResult.Yes Then

            If sSymbolName = "" Then
                RadMessageBox.Show("Please Select Revision Cloud!")
                Exit Sub
            End If

            Dim Datasource As LMADataSource
            Dim objPIDAutoApp As InteropPidAuto.Application
            Dim objAreaBreak As LMAreaBreak
            Dim objAreaBreaks As LMAreaBreaks
            Dim objAreaBreaksRevs As LMAreaBreaks
            Dim objItem As LMAItem
            Dim objPlacement As Placement
            Dim IndexRevisionCount As Integer
            Dim objPIDADrawing As InteropPidAuto.Drawing
            Dim i As Integer
            Dim sDrawigName As String

            IndexRevisionCount = 0
            Dim iIndex As Integer
            iIndex = 0

            ' show waitingBar
            Dim mythread As Thread
            mythread = New Thread(AddressOf ShowProgressBar)
            mythread.Start()

            'Datasource = New LMADataSource
            objPlacement = New Placement
            Try
                'On Error GoTo Failed
                objPIDAutoApp = CreateObject("pidautomation.Application")
                objPIDAutoApp.Visible = False
            Catch
                MessageBox.Show("Can't Open SPP&ID，Please check License or Whether sppid is corrupt")

                objPIDAutoApp = Nothing
                objPIDADrawing = Nothing
                Exit Sub
            End Try



            iTotal = DrawingCol.Count
            For i = 0 To iTotal - 1

                sDrawigName = DS.GetDrawing(DrawingCol.Item(i).ToString).Name.ToString


                Dim iRevNumber As String
                iRevNumber = Strings.Trim(RadTextRevisionNumber.Text)
                Dim filter As LMAFilter
                filter = New LMAFilter
                With filter.Criteria
                    .AddNew("1")
                    .Item("1").SourceAttributeName = "Drawing.Name"
                    .Item("1").Operator = "="
                    .Item("1").ValueAttribute = sDrawigName
                    .Item("1").Conjunctive = True
                    .AddNew("2")
                    .Item("2").SourceAttributeName = "Representation.FileName"
                    .Item("2").Operator = "="
                    .Item("2").ValueAttribute = sFilePath   ' \Equipment\Labels - Equipment\Equipment ID.sym
                    .Item("2").Conjunctive = True
                    '.AddNew("3")    '使用此过滤条件会报错，不合法的标准，所以就在后边判断RevisionNumber属性了
                    '.Item("3").SourceAttributeName = "Revision.RevisionNumber"
                    '.Item("3").Operator = "="
                    '.Item("3").ValueAttribute = iRevNumber
                    '.Item("3").Conjunctive = True
                    ''.AddNew("4")
                    '.Item("4").SourceAttributeName = "Package.PlantItemGroupType"   ’这个过滤条件也会报错，所以就使用下边的条件来筛选过Revision Cloud
                    '.Item("4").Operator = "="
                    '.Item("4").ValueAttribute = 65   ' Revision Cloud
                    '.Item("4").Conjunctive = True
                    .AddNew("3")
                    .Item("3").SourceAttributeName = "AreaBreakType"
                    .Item("3").Operator = "="
                    .Item("3").ValueAttribute = "2"   'Curve
                    .Item("3").Conjunctive = True

                End With

                filter.ItemType = "AreaBreak"
                objAreaBreaks = New LMAreaBreaks
                objAreaBreaks.Collect(DS, Filter:=filter）
                objAreaBreaksRevs = New LMAreaBreaks
                If objAreaBreaks.Count <> 0 Then
                    If Not iRevNumber = "" Then  '如果给了RevisionNumber
                        For Each objAreaBreak In objAreaBreaks
                            If Not objAreaBreak.Attributes("Revision.RevisionNumber") Is Nothing Then '排除没有放置Revision Triangle的云线，如果不放置，没有RevisionNumber属性
                                If Not IsDBNull(objAreaBreak.Attributes("Revision.RevisionNumber").Value) Then  '排除没有填写RevisionNumber属性的云线
                                    If objAreaBreak.Attributes("Revision.RevisionNumber").Value = iRevNumber Then
                                        objAreaBreaksRevs.Add(objAreaBreak)
                                        'IndexRevisionCount = IndexRevisionCount + 1
                                    End If
                                End If
                            End If
                        Next
                        If objAreaBreaksRevs.Count > 0 Then
                            Try
                                objPIDADrawing = objPIDAutoApp.Drawings.OpenDrawing(sDrawigName) '如果有满足条件的云线，开图删掉云线，否则不开图
                                If Not objPIDADrawing Is Nothing Then
                                    Try
                                        Dim blnDeleted As Boolean
                                        Dim place As Placement
                                        place = New Placement
                                        For Each objAreaBreak In objAreaBreaksRevs
                                            objItem = place.PIDDataSource.GetAreaBreak(objAreaBreak.Id).AsLMAItem
                                            blnDeleted = False
                                            blnDeleted = objPlacement.PIDDeleteItem(objItem) ' works OK !!!
                                        Next
                                        'LogList.Add(“Delete Revision Cloud“ & CONNMARK & m_DrawingList.Item(i) & "     RevisionNumber=" & iRevNumber & "     Delete Revision Cloud Successfully")

                                    Catch ex As Exception
                                        Writelog(“Delete Revision Cloud“ & sDrawigName & "    Error Delete Revision Cloud Failed" & ex.Message, "批量删除云线")
                                    End Try
                                    objPIDADrawing.CloseDrawing(True)
                                Else
                                    'LogList.Add(“Delete Revision Cloud“ & CONNMARK & m_DrawingList.Item(i) & "    Error Opening Drawing Failed")

                                End If
                            Catch ex As Exception
                                Writelog(“Delete Revision Cloud“ & sDrawigName & "    Can't Open this Drawing " & ex.Message, "批量删除云线")
                            End Try
                        Else
                            Writelog(“Delete Revision Cloud“ & sDrawigName & "    RevisionNumber=" & iRevNumber & "     Not Found in this Drawing", "批量删除云线")
                        End If
                    Else
                        '如果没有填写工具界面Revision Number，则忽略RevisionNumber的判定条件
                        Try
                            objPIDADrawing = objPIDAutoApp.Drawings.OpenDrawing(sDrawigName)
                            If Not objPIDADrawing Is Nothing Then
                                Try
                                    Dim blnDeleted As Boolean
                                    Dim place As Placement
                                    place = New Placement
                                    For Each objAreaBreak In objAreaBreaks
                                        objItem = place.PIDDataSource.GetAreaBreak(objAreaBreak.Id).AsLMAItem
                                        blnDeleted = False
                                        blnDeleted = objPlacement.PIDDeleteItem(objItem) ' works OK !!!
                                    Next
                                    'LogList.Add(“Delete Revision Cloud“ & CONNMARK & m_DrawingList.Item(i) & "         Delete All the Revision Cloud Successfully")
                                Catch ex As Exception
                                    Writelog(“Delete Revision Cloud. “ & sDrawigName & ". Error Delete Revision Cloud Failed." & ex.Message, "批量删除云线")
                                End Try

                                objPIDADrawing.CloseDrawing(True)
                            Else
                                'LogList.Add(“Delete Revision Cloud“ & CONNMARK & m_DrawingList.Item(i) & "    Error Opening Drawing Failed")
                            End If
                        Catch ex As Exception
                            Writelog(“Delete Revision Cloud. “ & sDrawigName & ". Can't Open this Drawing." & ex.Message, "批量删除云线")
                        End Try
                    End If
                Else
                    Writelog(“Delete Revision Cloud. “ & sDrawigName & ". No Revision Cloud Found in this Drawing.", "批量删除云线")
                End If

            Next

            Datasource = Nothing
            objPIDADrawing = Nothing
            objAreaBreak = Nothing
            objAreaBreaks = Nothing
            objAreaBreaksRevs = Nothing
            objItem = Nothing

            objPIDAutoApp.Quit()
            mythread.Abort()
        Else
            Writelog(“Delete Revision Cloud.“ & "   Terminate delete Revision Cloud ", "批量删除云线")
            Exit Sub
        End If
        Dim audit As New audit(DS.ProjectNumber, "批量删除云线", "V1.0")
        audit.Add()
        audit = Nothing
        MessageBox.Show("Finish Delete !")
    End Sub

    Private Sub ShowProgressBar()
        Dim ObjWaitingBar As New Waiting
        ObjWaitingBar.ShowDialog()
    End Sub


    ''' <summary>
    ''' 选择需要的sym
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim strSymbolpath As String = FindInfoInOptionManager("Catalog Explorer root path")
        Dim i As Integer, j As Integer
        Dim sFileWholePath As String
        Dim sFile() As String
        Dim RadOpenFileDialog2 As New RadOpenFileDialog
        With RadOpenFileDialog2

            .InitialDirectory = strSymbolpath
            .RestoreDirectory = True
            .Filter = "*.sym|*.sym"

        End With
        If RadOpenFileDialog2.ShowDialog() = DialogResult.OK Then

            sFileWholePath = RadOpenFileDialog2.FileName
        Else
            Exit Sub
        End If

        If sFileWholePath <> "" Then
            sFile = Split(sFileWholePath, "\")
            sSymbolName = sFile(UBound(sFile))

            i = Len(sFileWholePath)
            j = Len(strSymbolpath)
            sFilePath = Strings.Right(sFileWholePath, i - j)
        End If
        TextBox1.Text = sFilePath  '   \Equipment\Labels - Equipment\Equipment ID.sym
    End Sub
End Class
